package com.xiangyueEducation.uploaderCloud.aop;

import com.xiangyueEducation.uploaderCloud.context.BaseContext;
import com.xiangyueEducation.uploaderCloud.pojo.entity.baseEntity.BaseEntityUserId;
import com.xiangyueEducation.uploaderCloud.utils.Result;
import com.xiangyueEducation.uploaderCloud.utils.enums.ResultCodeEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ArticleOwnershipAspect {
// 加在
    @Around("@annotation(CheckArticleOwnership)")
    public Object checkArticleOwnership(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if (args.length > 0 && args[0] instanceof BaseEntityUserId) {
            BaseEntityUserId baseEntityUserId = (BaseEntityUserId) args[0];
            // BaseEntityUserId是否属于当前用户
            if (!baseEntityUserId.getUserId().equals(BaseContext.getCurrentUser().getId())) {
                // 如果不属于当前用户，则直接返回错误结果
                return Result.build(baseEntityUserId, ResultCodeEnum.ONLY_CAN_UPDATE_YOURSELF);
            }
        }
        // 如果检查通过，则执行原方法
        return joinPoint.proceed();
    }
}
